﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Harmonic.ReadModel;
using System.Web.Security;
using Harmonic.Models;

namespace Harmonic.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            var user = new RegisterModel();
            return View(user);
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(RegisterModel user)
        {
            try
            {
                if(ReadModel.Queries.UsernameExists(user.UserName))
                {
                    return View("Message", new Models.Message("This username already exists"));
                }

                CqrsService.Client.RegisterNewUser(user.UserName, user.Email, user.Password, 0);
                return View("Message", new Models.Message("The user has been created"));

            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }

        public ActionResult Login()
        {            
            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection collection)
        {
            string username = collection["Username"];

            using (ReadModel.ReadModel readModel = new ReadModel.ReadModel())
            {
                var password = (from n in readModel.Users
                            where n.Username == username
                           select n.Password).First();

                if (String.Equals(password, collection["Password"], StringComparison.OrdinalIgnoreCase))
                {
                    SetAuthenticationCookie(username);
                    return RedirectToAction("Index", "Home");
                }
                else
                    return View("Message", new Models.Message("Incorrect Password.")); 
            }
        }

        public virtual void SetAuthenticationCookie(string username)
        {
            FormsAuthentication.SetAuthCookie(username, false);
        }

    }
}
